package com.fengli.service.impl;

import com.fengli.model.User;
import com.fengli.service.UserService;
import com.fengli.vo.PageQueryResultVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class UserServiceImpl implements UserService {

    static List<User> datalist = new ArrayList<>();

    static {
        for (int i = 0; i < 15; i++) {
            User user = new User();
            user.setId(i + 1);
            user.setName("用户" + (i + 1));
            user.setAge(new Random().nextInt(100));
            datalist.add(user);
        }
    }

    @Override
    public PageQueryResultVo pageQuery(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<User> rows = new ArrayList<>();
        for (int i = start; i < start + pageSize; i++) {
            rows.add(datalist.get(i));
            // 兼容最后一页
            if (i >= datalist.size()) {
                break;
            }
        }

        PageQueryResultVo<User> resultVo = new PageQueryResultVo<>();
        resultVo.setRows(rows); // select id,name,age where 条件 limit start,pagesize
        resultVo.setTotal(datalist.size()); // // select count(*) from user where 条件
        return resultVo;
    }
}
